package memberrewards.service;

import hackathon1212.ServletPojoAdapter;
import java.util.HashMap;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.TierConfigDTO;
import static memberrewards.helpers.Constants.BLANK;

public class EnrollAccountDetail extends ServletPojoAdapter {
	private static final long serialVersionUID = 8166717310334174053L;

	protected Map<String,Object> getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan = params.get("pan")!=null? params.get("pan")[0]:null;
		final String tier = params.get("tier") != null ? params.get("tier")[0] : null;	
		//Default response
		final Map<String,Object> ret = new HashMap<String, Object>();
		ret.put("status", "Sorry,Unable to Enroll member to the system");
		if(pan!=null && !BLANK.equals(pan) && tier!=null && !BLANK.equals(tier)){
			//get the account details
			AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);		
			//get the Tier Info
			final TierConfigDTO tierConfig = RewardsDelegate.getTierConfig(tier);
			//Check card is valid
			if (accountDetail != null) {	
				ret.put("status", "Sorry,Unable to Enroll member,Member already Enrolled in System");
			}
			else{
				if(tierConfig == null){
					ret.put("status", "Sorry,You are trying to enroll to an unknown Tier,Please check the Tier");
				}
				else{
					accountDetail=new AccountDetailDTO();
					accountDetail.setPan(pan);
					accountDetail.setPointBalance("0");
					accountDetail.setTier(tier);
					final boolean opStatus = RewardsDelegate.enrollAccount(accountDetail);
					System.out.println(getClass().getName()+" enrollAccount Status ::"+opStatus);
					ret.put("status",opStatus?"Successfully Enrolled the Member":"Failed to Enroll the Member");
				}
			}
		}
		else{
			ret.put("status", "Any of the required input is missing");
		}
		return ret;
	}
}
